package br.com.smartrunner.web.managedbeans;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import org.apache.myfaces.custom.fileupload.UploadedFile;

import br.com.smartrunner.business.exceptions.AppException;

@ManagedBean
@RequestScoped
@SuppressWarnings("serial")
public class UploadBean extends AppManagedBean {

	private UploadedFile				uploadedFile;
	
	public String prepareUpload() {
		try {
			return "upload";
		} catch (Exception e) {
			return this.manageException(e);
		}
	}
	
	public String insert() {
		try {
			if (this.uploadedFile == null) {
				throw new AppException("fileIsMandatory");
			}
			
			InputStreamReader isr = new InputStreamReader(this.uploadedFile.getInputStream());
			BufferedReader br=new BufferedReader(isr);
			
			String line;
			while ((line = br.readLine()) != null) {
				System.out.println(line);
			}
			
			return "uploadNew";
		} catch (Exception e) {
			return this.manageException(e);
		}
	}
	
	// GETTERS AND SETTERS //
	public UploadedFile getUploadedFile() {
		return uploadedFile;
	}
	
	public void setUploadedFile(UploadedFile uploadedFile) {
		this.uploadedFile = uploadedFile;
	}
}
